package acwing.每日一题;


/**
 * @author： li
 * @date： 2022-03-17 21:59
 * @version 1.0
 */
import java.util.*;
public class 奶牛的体操 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int K,N;
        K = sc.nextInt();
         N = sc.nextInt();
        int[][] rank = new int[K+1][N+1];
        int[] cnt = new int[N+1];
        for(int i=1;i<=K;i++)
            for(int j=1;j<=N;j++){
                int r = sc.nextInt();
                rank[i][r] = j;
            }
        for(int i=1;i<=K;i++){
            for(int j=1;j<=N;j++){
                System.out.print(rank[i][j]+ " ");
            }
            System.out.println(" ");
        }
        int ans=0;int count=0,count1=0;
        if(K>1) {
            for (int i = 1; i <= N; i++) {
                for (int l = i + 1; l <= N; l++) {
                    for (int j = 1; j <= K; j++) {
                        if (rank[j][i] < rank[j][l]) {
                            count++;
                        } else {
                            count1++;
                        }
                        System.out.println("count="+count);
                        if (count == K || count1 == K) {
                            ans++;
                        }
                        if (j == K) {
                            count = 0;
                            count1 = 0;
                        }
                    }
                }
            }
        }else {
            for(int j=1;j<=N;j++)
            for(int i=1;i<=N;i++){
                if(i==j)continue;
                if(rank[1][j]<rank[1][i]){
                    ans++;
                }
            }
        }
        System.out.println(ans);
        sc.close();
    }
}
